import json

from bs4 import BeautifulSoup
from loguru import logger


class MajorSchoolParser:
    def __init__(self):
        super().__init__()

    def parse_school_major_list(self, html, school_name):
        soup = BeautifulSoup(html, "lxml")
        div_tags = soup.find_all("div", {"class": "kaishezhuanye"})
        zy_table = div_tags[0].find("table", {"class": "layui-table"})
        zy_tr_arr = zy_table.find("tbody").find_all("tr")
        major_list_dic = {}

        for zy_tr in zy_tr_arr:
            td_arr = zy_tr.find_all("td")
            depart_name = td_arr[0].text
            p_arr = td_arr[1].find_all("p")

            for p in p_arr:
                a_tag = p.find("a")
                major_list_item = {
                    "university_name": school_name,
                    "major_name": p.text,
                    "major_code": a_tag.attrs["href"].split("/")[-1].split(".")[0],
                    "main_major": 0,
                    "university_department": depart_name
                }
                major_list_dic[depart_name + p.text] = major_list_item

        zdzy_table = div_tags[1].find("table", {"class": "layui-table"})
        zdzy_tr_arr = zdzy_table.find("tbody").find_all("tr")
        for zdzy_tr in zdzy_tr_arr:
            td_arr = zdzy_tr.find_all("td")
            depart_name = td_arr[0].text
            p_arr = td_arr[1].find_all("p")
            for p in p_arr:
                zdzy = p.text
                major_list_item = major_list_dic[depart_name + zdzy]
                major_list_item["main_major"] = 1

        return major_list_dic.values()
